//
//  CenterMenuView.swift
//  QQ空间
//
//  Created by 叶竞泽 on 2017/5/9.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

enum CenterMenuViewItemType {
    case feed
    case passive
    case pic
    case album
    case friend
    case more
}

protocol CenterMenuViewDelegate: NSObjectProtocol {
    func didClickMenuItem(withType type: CenterMenuViewItemType, fromButton withTag: Int)
}

class CenterMenuView: UIView {

    // MARK: - 属性
    
    /// 父视图
    var inView: UIView
    /// 底部视图(下方的视图)
    var bottomView: UIView
    /// 子控件按钮数组
    var subButtons: [UIButton] = [UIButton]()
    /// 子控件按钮title数组
    var subButtonTitles: [String] {
        return ["全部动态", "与我相关", "照片墙", "电子相框", "好友", "其他"]
    }
    /// 子控件按钮图片数组
    var subButtonImages: [String] {
        return ["tab_bar_feed_icon", "tab_bar_passive_feed_icon", "tab_bar_pic_wall_icon", "tab_bar_e_album_icon", "tab_bar_friend_icon", "tab_bar_e_more_icon"]
    }
    /// 当前选中的子控件按钮
    var currentSeletedButton: UIButton?
    /// 代理
    weak var delegate: CenterMenuViewDelegate?
    
    /// 不同设备方向时视图的高度
    private var heightForPortrait: CGFloat {
        return inView.bounds.height * percentHeightInPortrait
    }
    private var heightForLandscape: CGFloat {
        return inView.bounds.height * percentHeightInLandscape
    }
    
    /// 宽度在父视图的比例(竖屏)
    private var percentHeightInPortrait: CGFloat = 0.5
    /// 宽度在父视图的宽度(横屏)
    private var percentHeightInLandscape: CGFloat = 0.6
    
    // MARK: - 生命周期
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layoutButtons()
    }
    
    /// 初始化
    ///
    /// - Parameter inView: 父视图
    init(inView: UIView, bottomView: UIView) {
        self.inView = inView
        self.bottomView = bottomView
        super.init(frame: CGRect.zero)
        // 根据设备方向设置frame
        changeOrientation()
        // 创建子控件按钮
        createSubButton()
        // 监听屏幕旋转
        NotificationCenter.default.addObserver(self, selector: #selector(changeOrientation), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
        // 初始选中状态
        buttonClick(sender: subButtons.first)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 方法
    
    /// 设置按钮选中
    fileprivate func setButtonSeleted(button: UIButton?) {
        button?.isSelected = true
        button?.backgroundColor = #colorLiteral(red: 0.2666400671, green: 0.2666836977, blue: 0.2666305304, alpha: 1)
    }
    
    /// 监听设备旋转
    @objc private func changeOrientation() {
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            frame = CGRect(x: 0, y: inView.bounds.height - heightForPortrait - bottomView.bounds.height, width: inView.bounds.width, height: heightForPortrait)
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            frame = CGRect(x: 0, y: inView.bounds.height - heightForLandscape - bottomView.bounds.height, width: inView.bounds.width, height: heightForLandscape)
        default:
            break
        }
    }
    
    @objc fileprivate func buttonClick(sender: UIButton?) {
        for button in subButtons {
            button.isSelected = false
            button.backgroundColor = UIColor.black
        }
        sender?.isSelected = true
        sender?.backgroundColor = #colorLiteral(red: 0.2666400671, green: 0.2666836977, blue: 0.2666305304, alpha: 1)
        currentSeletedButton = sender
        guard let tag = sender?.tag else { return }
        switch tag {
        case 0:
            delegate?.didClickMenuItem(withType: .feed, fromButton: tag)
        case 1:
            delegate?.didClickMenuItem(withType: .passive, fromButton: tag)
        case 2:
            delegate?.didClickMenuItem(withType: .pic, fromButton: tag)
        case 3:
            delegate?.didClickMenuItem(withType: .album, fromButton: tag)
        case 4:
            delegate?.didClickMenuItem(withType: .friend, fromButton: tag)
        case 5:
            delegate?.didClickMenuItem(withType: .more, fromButton: tag)
        default:
            break
        }
    }
}

// MARK: - 设置界面
extension CenterMenuView {
    /// 添加子控件按钮
    fileprivate func createSubButton() {
        for i in 0 ..< 6 {
            let button = UIButton()
            button.tag = i
            button.setTitleColor(UIColor.white, for: .normal)
            button.setImage(UIImage(named: subButtonImages[i]), for: .normal)
            button.adjustsImageWhenHighlighted = false // 取消点击效果
            button.addTarget(self, action: #selector(buttonClick(sender:)), for: .touchUpInside)
            subButtons.append(button)
            addSubview(button)
        }
    }
    
    /// 根据设备方向布局子控件按钮
    fileprivate func layoutButtons() {
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            for i in 0 ..< subButtons.count {
                let w = bounds.width
                let h = bounds.height / CGFloat(subButtons.count)
                let y = h * CGFloat(i)
                subButtons[i].frame = CGRect(x: 0, y: y, width: w, height: h)
                subButtons[i].setTitle("", for: .normal)
                subButtons[i].imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
            }
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            for i in 0 ..< subButtons.count {
                let w = bounds.width
                let h = bounds.height / CGFloat(subButtons.count)
                let y = h * CGFloat(i)
                subButtons[i].frame = CGRect(x: 0, y: y, width: w, height: h)
                subButtons[i].setTitle(subButtonTitles[i], for: .normal)
                subButtons[i].imageEdgeInsets = UIEdgeInsetsMake(0, -(subButtons[i].frame.width / 11), 0, subButtons[i].frame.width / 11)
            }
        default:
            break
        }
    }
}
